/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.config;

import com.jspx.sober.TableModels;
import com.jspx.sober.annotation.IDType;
import com.jspx.utils.StringUtil;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.XMLUtil;

import java.util.*;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-1-6
 * Time: 23:07:57
 */
public class SoberTable implements TableModels, Serializable {
    //表名
    private String tableName = StringUtil.empty;
    //表别名
    private String tableCaption = StringUtil.empty;

    //是否动态创建表
    private boolean create = true;

    //cache
    private boolean useCache = true;
    //cache name 如果没有就使用默认的 sober里边默认配置的cache
    private String cacheName;

    //实体,具体的类
    private Class entity;
    //关键字名
    private String primary = StringUtil.empty;
    //是否自动生成ID
    private boolean autoId = true;
    //是否使用数据库自增
    private String idType = StringUtil.empty;
    //映射对应关系
    private Map<String, SoberNexus> nexusMap = new LinkedHashMap<String, SoberNexus>();
    //字段
    private List<SoberColumn> columns = new LinkedList<SoberColumn>();
    //字段
    private Map<String, SoberCalcUnique> calcUniqueMap = new LinkedHashMap<String, SoberCalcUnique>();
    //最后访问时间
    private long lastDate = System.currentTimeMillis();

    /**
     * 得到表名
     *
     * @return String
     */
    public String getTableName() {
        if (StringUtil.isNULL(tableName)) {
            tableName = StringUtil.substringAfterLast(entity.getName(), ".");
        }
        return tableName;
    }

    public String getTableCaption() {
        if (StringUtil.isNULL(tableCaption)) {
            return getTableName();
        }
        return tableCaption;
    }

    public void setTableCaption(String tableCaption) {
        this.tableCaption = tableCaption;
    }

    public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    public boolean isSerial() {
        return IDType.serial.equalsIgnoreCase(idType);
    }

    /**
     * 设置表名
     *
     * @param tableName 表名
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Class getEntity() {
        return entity;
    }

    public void setEntity(Class entity) {
        this.entity = entity;
    }

    public String getPrimary() {
        return primary;
    }

    public void setPrimary(String primary) {
        this.primary = primary;
    }

    public boolean isAutoId() {
        return autoId;
    }

    public void setAutoId(boolean autoId) {
        this.autoId = autoId;
    }

    public List<SoberColumn> getColumns() {
        return columns;
    }

    public void setColumns(List<SoberColumn> columns) {
        this.columns = columns;
    }


    public SoberColumn getColumn(String keys) {
        for (SoberColumn column:columns)
        {
            if (column.getName().equalsIgnoreCase(keys)) return column;
        }
        return null;
    }

    public boolean containsField(String keys)
    {
        for (SoberColumn column:columns)
        {
            if (column.getName().equalsIgnoreCase(keys)) return true;
        }
        return false;
    }

    public Map<String, SoberCalcUnique> getCalcUniqueMap() {
        return calcUniqueMap;
    }

    public void setCalcUniqueMap(Map<String, SoberCalcUnique> calcUniqueMap) {
        this.calcUniqueMap = calcUniqueMap;
    }

    public String[] getFieldArray() {
        String[] fieldArray = null;
        for (SoberColumn column : columns) {
            if (IDType.serial.equalsIgnoreCase(idType) && column.getName().equals(primary)) continue;
            fieldArray = ArrayUtil.add(fieldArray, column.getName());
        }
        return fieldArray;
    }

    public String[] getFullFieldArray() {

        String[] fieldArray = null;
        for (SoberColumn column : columns) {
            fieldArray = ArrayUtil.add(fieldArray, column.getName());
        }
        return fieldArray;
    }


    public Map<String, SoberNexus> getNexusMap() {
        return nexusMap;
    }

    public void setNexusMap(Map<String, SoberNexus> nexusMap) {
        this.nexusMap = nexusMap;
    }

    public long getLastDate() {
        return lastDate;
    }

    public void setLastDate(long lastDate) {
        this.lastDate = lastDate;
    }

    public void updateLastDate() {
        this.lastDate = System.currentTimeMillis();
    }

    public boolean isUseCache() {
        return useCache;
    }

    public void setUseCache(boolean useCache) {
        this.useCache = useCache;
    }

    public String getCacheName() {
        return cacheName;
    }

    public void setCacheName(String cacheName) {
        this.cacheName = cacheName;
    }

    public boolean isCreate() {
        return create;
    }

    public void setCreate(boolean create) {
        this.create = create;
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("<table caption=\"").append(tableCaption).append("\" ");
        sb.append("name=\"").append(tableName).append("\" ");
        sb.append("create=\"").append(create).append("\" ");
        sb.append("useCache=\"").append(useCache).append("\" ");
        sb.append("cacheName=\"").append(cacheName).append("\"/>");
        return  sb.toString();
    }
}